package cn.edu.scau.cmi.zhuangzhihong.hibernate.client;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TreeTableColumn;

import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import cn.edu.scau.cmi.zhuangzhihong.hibernate.dao.Student;
import cn.edu.scau.cmi.zhuangzhihong.hibernate.dao.StudentHome;
import cn.edu.scau.cmi.zhuangzhihong.hibernate.dao.StudentInformation;
import cn.edu.scau.cmi.zhuangzhihong.hibernate.dao.Teacher;
import cn.edu.scau.cmi.zhuangzhihong.hibernate.dao.TeacherHome;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.scene.control.TableView;

import javafx.scene.control.TableColumn;

public class viewController implements Initializable{
	@FXML
	private TextField TeacherId1;
	@FXML
	private TextField StudentId;
	@FXML
	private TextField StudentName;
	@FXML
	private TextField TeacherName1;
	@FXML
	private Button UpdateStudent;
	@FXML
	private Button PickTeacher;
	@FXML
	private Button AddTeacher;
	@FXML
	private Button DeleteStudent;
	@FXML
	private Button ChangeTeacher;
	@FXML
	private Button UpdateTeacher;
	@FXML
	private Button DeleteTeacher;
	@FXML
	private TextField TeacherName;
	@FXML
	private TextField TeacherId;
	@FXML
	private TextField StudentName1;
	@FXML
	private TextField StudentId1;
	@FXML
	private Button ChangeStudent;
	@FXML
	private Button PickStudent;

	@FXML
	private TreeTableColumn TeacherTree;
	@FXML
	private TreeTableColumn StudentTree;
	@FXML
	private TableView<StudentInformation> TableView;
	@FXML
	private TableColumn<StudentInformation, Long> StudentTableId;
	@FXML
	private TableColumn<StudentInformation,String> StudentTableName;
	@FXML
	private Button Refresh;
	
	// Event Listener on Button.onAction
	 public void EventAddStudent(ActionEvent event) {
		 String id=StudentId.getText();
		
		 String name=StudentName.getText();
		 Student student=new Student(name);
		System.out.println(student);
		
			StudentHome sh = new StudentHome();
			sh.attachDirty(student);
			sh.comple();
	    }
	 
	 public void EventUpdateStudent(ActionEvent event) {
		 String id=StudentId.getText();
		 Long idl=Long.valueOf(id);
		 
		 String name=StudentName.getText();
		 StudentHome sh = new StudentHome();
		 Student student=sh.findById(idl);
		 
		 student.setName(name);
		 
		 sh.attachDirty(student);
		 sh.comple();
	 }
	
	 public void  EventDeleteStudent(ActionEvent event) {
		 String id=StudentId.getText();
		 Long idl=Long.valueOf(id);
		 
		 String name=StudentName.getText();
		 StudentHome sh = new StudentHome();
//		 Student student1=new Student(name);
//		 student1=sh.findById(idl);
		
		 Student student2=new Student(name);
		 List<Student> list=new ArrayList();
		 student2.setId(idl);
		 student2.setName(name);
		 list=sh.findByExample(student2);
		 sh.delete(list.get(0));
		 sh.comple();
	 }
	 
	// Event Listener on Button.onAction
		 public void EventAddTeacher(ActionEvent event) {
			 String id=TeacherId.getText();
			
			 String name=TeacherName.getText();
			 Teacher teacher=new Teacher(name);
			
			 TeacherHome sh = new TeacherHome();
				sh.attachDirty(teacher);
				sh.comple();
		    }
		 
		 public void EventUpdateTeacher(ActionEvent event) {
			 String id=TeacherId.getText();
			 Long idl=Long.valueOf(id);
			 TeacherHome sh = new TeacherHome();
			 String name=TeacherName.getText();
			 Teacher teacher=sh.findById(idl);
			 teacher.setName(name);
			 
			 sh.attachDirty(teacher);
			 sh.comple();
		 }
		
		 public void  EventDeleteTeacher(ActionEvent event) {
			 String id=TeacherId.getText();
			 Long idl=Long.valueOf(id);
			 
			 String name=TeacherName.getText();
			 TeacherHome sh = new TeacherHome();
//			 Student student1=new Student(name);
//			 student1=sh.findById(idl);
			
			 Teacher teacher2=new Teacher(name);
			 List<Teacher> list=new ArrayList();
			 teacher2.setId(idl);
			 teacher2.setName(name);
			 list=sh.findByExample(teacher2);
			 sh.delete(list.get(0));
			 sh.comple();
		 }
		 
		 public void  EventChooseTeacher(ActionEvent event) {
			 String id=TeacherId1.getText();
			 Long idl=Long.valueOf(id);
			 
			 String name=TeacherName1.getText();
			 TeacherHome th = new TeacherHome();
//			 Student student1=new Student(name);
//			 student1=sh.findById(idl);
			
			 Teacher teacher2=new Teacher(name);
			 List<Teacher> list=new ArrayList();
			 teacher2.setId(idl);
			 teacher2.setName(name);
			 list=th.findByExample(teacher2);
			 teacher2=list.get(0);
			 
			 String sid=StudentId.getText();
			 Long id2=Long.valueOf(sid);
			 
			 String sname=StudentName.getText();
			 StudentHome sh = new StudentHome();
//			 Student student1=new Student(name);
//			 student1=sh.findById(idl);
			
			 Student student2=new Student(name);
			 List<Student> list2=new ArrayList();
			 student2.setId(id2);
			 student2.setName(sname);
			 list2=sh.findByExample(student2);
//			 System.out.println(student2.toString());
			student2=list2.get(0);
			
			 student2.setTeacher(teacher2);
			 
			 sh.attachDirty(student2);
			 sh.comple();
		 }
		 
		 public void  EventChooseStudent(ActionEvent event) {
			 String id=TeacherId.getText();
			 Long idl=Long.valueOf(id);
			 
			 String name=TeacherName.getText();
			 TeacherHome th = new TeacherHome();
//			 Student student1=new Student(name);
//			 student1=sh.findById(idl);
			
			 Teacher teacher=new Teacher(name);
			 List<Teacher> list=new ArrayList();
			 teacher.setId(idl);
			 teacher.setName(name);
			 list=th.findByExample(teacher);
			 teacher=list.get(0);
			 
			 String sid=StudentId1.getText();
			 Long id2=Long.valueOf(sid);
			 
			 String sname=StudentName1.getText();
			 StudentHome sh = new StudentHome();
//			 Student student1=new Student(name);
//			 student1=sh.findById(idl);
			
			 Student student=new Student(name);
			 List<Student> list2=new ArrayList();
			 student.setId(id2);
			 student.setName(sname);
			 list2=sh.findByExample(student);
//			 System.out.println(student2.toString());
			student=list2.get(0);
			 
			 student.setTeacher(teacher);
			 
			 sh.attachDirty(student);
			 sh.comple();
		 }
		 
		 public void  EventChangeStudent(ActionEvent event) {
			 
			
			
			 
			 String sid=StudentId1.getText();
			 Long id2=Long.valueOf(sid);
			 
			 String sname=StudentName1.getText();
			 StudentHome sh = new StudentHome();
//			 Student student1=new Student(name);
//			 student1=sh.findById(idl);
			
			 Student student=new Student(sname);
			 List<Student> list2=new ArrayList();
			 student.setId(id2);
			 student.setName(sname);
			 list2=sh.findByExample(student);
//			 System.out.println(student2.toString());
			student=list2.get(0);
			
			 student.setTeacher(null);
			 System.out.println(student);
			 sh.attachDirty(student);
			 sh.comple();
		 }
		 
			@FXML
			private TreeView<String> TableTree;
		 
		
		    private void initTreeView(){
		    	
		    	//父节点
		        TreeItem<String> item = new TreeItem<>("教师列表");
		        TableTree.setRoot(item);
		        item.setExpanded(false);
		        
		        TeacherHome th=new TeacherHome();
		        List<Teacher> list = new ArrayList();
		        list=th.FindAll();
		        int i=0;
		        //获取教师列表
		        for(Teacher t:list) {
		        	TreeItem<String> k = new TreeItem<>(t.getId()+" "+t.getName());
		        	Set<Student> set = new HashSet();
		        	set=t.getStudents();
		        	k.setExpanded(false);
		        	//获取学生
		        	for(Student s:set) {
		        		TreeItem<String> ss = new TreeItem<>(s.getId()+" "+s.getName());
		        		k.getChildren().addAll(ss);
		        		}
		        	
		        	item.getChildren().addAll(k);
		        }
//		        TreeItem<String> i1 = new TreeItem<>("电影");
//		        TreeItem<String> i2 = new TreeItem<>("音乐");
//		        TreeItem<String> i3 = new TreeItem<>("游戏");
//		        item.getChildren().addAll(i1,i2,i3);
//		        TreeItem<String> i4 = new TreeItem<>("荡寇风云");
//		        TreeItem<String> i5 = new TreeItem<>("变形金刚5");
//		        i1.setExpanded(false);
//		        i1.getChildren().addAll(i4,i5);
		        
		    }
		 
		 public void showList() {
			 ObservableList<StudentInformation> list = FXCollections.observableArrayList();
			
			 
				StudentHome th = new StudentHome();
//				Student student=th.findById(56l);
				List<Student> studentlist=(List<Student>)th.FindAll();
//				System.out.println(studentlist.size());
//				
				for(Student s : studentlist) {
					
//					System.out.println(s.toString());
					list.add(new StudentInformation(s.getId(),s.getName(),s.getTeacher()));
//					
				}
				StudentTableId.setCellValueFactory(cellData->cellData.getValue().getSid().asObject());
				StudentTableName.setCellValueFactory(cellData->cellData.getValue().getSname());
				TableView.setItems(list);
				th.comple();
				initTreeView();
		 }
		@Override
		public void initialize(URL location, ResourceBundle resources) {
			// TODO 自动生成的方法存根
			showList();
			initTreeView();
		}
}
